package com.javajun.controller;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.javajun.entity.Claxx;
import com.javajun.entity.Manager;
import com.javajun.entity.Student;
import com.javajun.form.StudentForm;
import com.javajun.service.ClaxxService;
import com.javajun.service.StudentService;

/**
 * @ClassName: StudentController
 * @Description: 学生控制器
 * @author: xlm
 * @date: 2018年10月15日
 */
@Controller
@RequestMapping(value = "/admin/student")
public class StudentController {

	@Autowired
	private StudentService studentService;
	@Autowired
	private ClaxxService claxxService;
	
	// 显示所有学生
	@RequestMapping(value = "/manage")
	public String manager(ModelMap map, StudentForm form) {
		List<Student> models = studentService.findAll(BuildSpec(form));
		List<Claxx> claxxs = claxxService.findAll();
		// 搜索条件
		map.put("searchs", form);
		// 所有班级
		map.put("claxxs", claxxs);
		// 结果
		map.put("models", models);
		return "student/manage";
	}

	// 修改页面
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String Edit(ModelMap map, Integer id) {
		Student model = new Student();
		if (id != null) {
			model = studentService.findById(id);
		}
		// 修改页面需要班级
		List<Claxx> models = claxxService.findAll();
		map.put("models", models);
		map.put("model", model);
		return "student/edit";
	}

	// 保存方法
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String Save(ModelMap map, Student entity) {
		Student student = new Student();
		if (entity.getId() != null) {
			student = studentService.findById(entity.getId());
		}
		if (entity.getStuNum() != null) {
			student.setStuNum(entity.getStuNum());
		}
		if (entity.getName() != null) {
			student.setName(entity.getName());
		}
		if (entity.getSex() != null) {
			student.setSex(entity.getSex());
		}
		if (entity.getJob() != null) {
			student.setJob(entity.getJob());
		}
		if (entity.getClaxx().getId() != null) {
			Claxx claxx = new Claxx();
			claxx = claxxService.findById(entity.getClaxx().getId());
			student.setClaxx(claxx);
		}
		if (entity.getPassword() != null) {
			student.setPassword(entity.getPassword());
		}
		studentService.save(student);
		return "redirect:manage";
	}
	
	// 新增页面
	@RequestMapping(value="/add")
	public String Add(ModelMap map) {
		// 修改页面需要班级
		List<Claxx> models = new ArrayList<>();
		models = claxxService.findAll();
		map.put("models", models);
		return "student/add";
	}
	
	// 删除一个学生
	@RequestMapping(value="/delete")
	public String Delete(ModelMap map, Integer id) {
		if(id != null) {
			Student entity = new Student();
			entity = studentService.findById(id);
			studentService.delete(entity);
		}
		return "redirect:manage";
	}
	
	// 动态安全条件生成
	public Specification<Student> BuildSpec(StudentForm form) {

		Specification<Student> spec = new Specification<Student>() {

			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<>();
				predicates.add(cb.greaterThan(root.get("id"), 0));
				if (StringUtils.hasText(form.getName())) {
					predicates.add(cb.like(root.get("name"), "%" + form.getName() + "%"));
				}
				if (StringUtils.hasText(form.getSex())) {
					predicates.add(cb.equal(root.get("sex"), form.getSex()));
				}
				if (StringUtils.hasText(form.getJob())) {
					predicates.add(cb.like(root.get("job"), "%" + form.getJob() + "%"));
				}
				if (form.getClaxxId() != null) {
					predicates.add(cb.equal(root.get("claxx").get("id"), form.getClaxxId()));
				}
				return cb.and(predicates.toArray(new Predicate[predicates.size()]));
			}
		};
		return spec;
	}
}
